package com.example.mqtt.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.integration.mqtt.event.MqttConnectionFailedEvent;
import org.springframework.integration.mqtt.event.MqttMessageDeliveredEvent;
import org.springframework.integration.mqtt.event.MqttMessageSentEvent;
import org.springframework.integration.mqtt.event.MqttSubscribedEvent;
import org.springframework.stereotype.Component;

/**
 * 事件侦听
 *
 * @author：yu8home
 * @date：2025年7月14日 下午3:44:04
 */
@Component
@Slf4j
public class MqttListener {

    @EventListener(classes = MqttConnectionFailedEvent.class)
    public void listenerAction(MqttConnectionFailedEvent event) {
        log.info("连接失败");
    }

    @EventListener(classes = MqttMessageSentEvent.class)
    public void listenerAction(MqttMessageSentEvent event) {
        log.info("已发送");
    }

    /**
     * <pre>
     * QOS 0：发送消息后会立即回调，因为不需要等待回执
     * QOS 1：发送消息后会等待ACK回执，ACK回执后会进行此事件通知
     * QOS 2：发送消息后会等待PubRECV回执，直到PubCOMP后会进行此事件通知
     * </pre>
     */
    @EventListener(classes = MqttMessageDeliveredEvent.class)
    public void listenerAction(MqttMessageDeliveredEvent event) {
        log.info("已传输");
    }

    @EventListener(classes = MqttSubscribedEvent.class)
    public void listenerAction(MqttSubscribedEvent event) {
        log.info("已订阅");
    }

}
